package com.text.demmongodb.controller;

import com.mongodb.client.MongoCollection;
import com.text.demmongodb.entity.User;
import com.text.demmongodb.mapper.UserMapper;
import com.text.demmongodb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.bson.Document;

import java.util.Date;
import java.util.List;

/**
 * @ClassName UserController
 * @Author By wen
 * @Description
 * @Date 2023/10/30 11:27
 * @Version 1.0
 */
@Controller
@RequestMapping("/user/*")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("insert/{id}/{name}/{age}")
    @ResponseBody
    public String insert(@PathVariable("id")String id, @PathVariable("name")String name,@PathVariable("age")int age){
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setAge(age);
        return userService.insert(user);
    }

    @GetMapping("updateAge/{id}/{age}")
    @ResponseBody
    public String updateAge(@PathVariable("id")String id, @PathVariable("age")int age){
        User user = new User();
        user.setId(id);
        user.setAge(age);
        return userService.updateUserInfo(user);
    }

    @GetMapping("getById/{id}")
    @ResponseBody
    public String getById(@PathVariable("id")String id){
        User user = new User();
        user.setId(id);
        return userService.getById(user);
    }

    /**
     * 获取分页list
     * @return
     */
    @GetMapping("getList")
    @ResponseBody
    public String getList(){
        return userService.getList();
    }


    /**
     * 根据age查询大于该age的list
     * @param age
     * @return
     */
    @GetMapping("getListByAge/{age}")
    @ResponseBody
    public String getListByAge(@PathVariable("age")int age){
        User user = new User();
        user.setAge(age);
        return userService.getListByAge(user);
    }

    /**
     * 根据age查询大于该age的list
     * @param
     * @return
     */
    @GetMapping("delete/{id}")
    @ResponseBody
    public String deleteById(@PathVariable("id")String id){
        User user = new User();
        user.setId(id);
        return userService.deleteById(user);
    }
}
